package com.matuw.android.util.extensions

import android.view.LayoutInflater
import android.view.View
import android.view.View.OnClickListener
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import com.matuw.android.util.ViewUtils
import com.matuw.android.util.ViewUtils.DebounceClickListener


/**
 * 防重复点击
 */
fun View.debounceClick(listener: OnClickListener) {
    debounceClick(ViewUtils.getDebounceClickInterval(), listener)
}

/**
 * 防重点
 * @param interval 延时 单位:ms
 */
fun View.debounceClick(interval: Long, listener: OnClickListener) {
    ViewUtils.debounceClick(this, interval, listener)
}

/**
 * 多个View共同防重复点击
 */
fun <T : View> Array<T>.debounceClick(listener: OnClickListener) {
    debounceClick(ViewUtils.getDebounceClickInterval(), listener)
}

fun <T : View> Array<T>.debounceClick(interval: Long, listener: OnClickListener) {
    val l = DebounceClickListener(interval, listener)
    forEach { it.setOnClickListener(l) }
}

@JvmOverloads
fun ViewGroup.inflate(@LayoutRes resource: Int, attachToRoot: Boolean = false): View {
    return LayoutInflater.from(context).inflate(resource, this, attachToRoot)
}
